package com.kangye.exam.service.serviceimpl;

import com.kangye.exam.bean.Paper;
import com.kangye.exam.bean.Student;
import com.kangye.exam.bean.StudentPaper;
import com.kangye.exam.dao.PaperDao;
import com.kangye.exam.dao.StudentpaperDao;
import com.kangye.exam.service.StudentTestService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service("sts")
public class studentExamServiceImpl implements StudentTestService {

    @Resource
    private PaperDao paperDao;
    @Resource
    private StudentpaperDao std;

    //所有没有写过的试卷的加载
    @Override
    public List<Paper> testLoading(Student student) {
        List<Integer> paperList = new ArrayList<>();
        List<Integer> stuPaperList = new ArrayList<>();
        paperList = paperDao.getPaperIdList(student);//获取所有的试卷id
        stuPaperList = std.getStupaperIdList(student);//获取所有的做过的试卷id
        int i = 0;
        List<Integer> paperIdList = new ArrayList<>();
        for (Integer pid: paperList) {           //进行对比得出未做过的试卷id的集合
            for (Integer spId: stuPaperList) {
                if (pid==spId){
                    i++;
                }
            }
            if (i==0){
                paperIdList.add(pid);
            }
            i=0;
        }//获得未做过的试卷的集合
        //做判断为空时不进行？？？
        if (paperIdList.size()==0){
            return null;
        }
        return paperDao.getPapers(paperIdList);
    }

    //将学生答案添加进数据库中
    @Override
    public boolean addAnswer(StudentPaper studentpaper) {
        int i = std.addAnswer(studentpaper);
        if (i==0){
            return false;
        }
        return true;
    }

    //登录测试
    @Override
    public Student login(Student student) {
        Student student1 = std.login(student);
        return student1;
    }

    @Override
    public Paper getPaper(Integer paper_id) {
        Paper paper = new Paper();
        paper.setPaperId(paper_id);
        paper = paperDao.getPaper(paper);
        return paper;
    }

    @Override
    public Student getStudent(Integer stuId) {
        Student student = new Student();
        student.setStudentId(stuId);
        return paperDao.getStudentName(student);
    }
}
